package ATM;

import java.io.*;
import java.util.*;

public class FileOperation {

    public static HashMap<String, UserInfo> readFile() {
        HashMap<String, UserInfo> allUsers = new HashMap<>();

        try (FileInputStream fis = new FileInputStream("user.data");
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            allUsers = (HashMap<String, UserInfo>) ois.readObject();
        } catch (FileNotFoundException e) {
            //可添加
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return allUsers;
    }

    public static void writeFile(HashMap<String, UserInfo> allUsers) {
/*        allUsers = new HashMap<>();

        UserInfo user1 = new UserInfo("zhang3", "123456", 10000);
        UserInfo user2 = new UserInfo("li4", "123456", 10000);
        UserInfo user3 = new UserInfo("wang5", "123456", 10000);
        UserInfo user4 = new UserInfo("zhao6", "123456", 10000);

        allUsers.put("zhang3", user1);
        allUsers.put("li4", user2);
        allUsers.put("wang5", user3);
        allUsers.put("zhao6", user4);*/

        try (FileOutputStream fos = new FileOutputStream("user.data");
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(allUsers);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
